package com.hxd.controller;

import com.hxd.pojo.Department;
import com.hxd.pojo.Page;
import com.hxd.pojo.User_duty;
import com.hxd.pojo.Users;
import com.hxd.service.DepartmentService;
import com.hxd.service.UserService;
import com.hxd.service.User_dutyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@RequestMapping("/personnel")
@Controller
public class personnelController {
    @Autowired
    UserService userService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    User_dutyService user_dutyService;
    //进入添加员工信息页面
    @RequestMapping("insert")
    public String insert(Model model) {
        //查询所有职位信息
        List<User_duty> user_dutyDaos = user_dutyService.selectUser_duty();
        model.addAttribute("user_dutyDaos",user_dutyDaos);
        //查找所有部门信息
        List<Department> departments = departmentService.selectAlldepartment();
        model.addAttribute("departments", departments);
        return "person_manage/员工添加";
    }

    //添加员工信息
    @RequestMapping("add")
    public String add(Users users, Model model) {
        Users users1 = userService.selectUser(users.getUsername());
        if(users1!=null){
            //查询所有职位信息
            List<User_duty> user_dutyDaos = user_dutyService.selectUser_duty();
            model.addAttribute("user_dutyDaos",user_dutyDaos);
            //查找所有部门信息
            List<Department> departments = departmentService.selectAlldepartment();
            model.addAttribute("departments", departments);
            model.addAttribute("msg","账号已存在！");
            return "person_manage/员工添加";
        }
        //插入一条员工信息
        int i = userService.insertuser(users);
        if(i>0){
            System.out.println("插入成功！");
        }else{
            System.out.println("插入失败！");
        }
        return "redirect:/personnel/insert";
    }

    //所有员工信息(分页查)
    @RequestMapping("select")
    public String select(Model model, @RequestParam(defaultValue = "1") int pagenum, Users users1) {
        //查询所有职位信息
        List<User_duty> user_dutyDaos = user_dutyService.selectUser_duty();
        model.addAttribute("user_dutyDaos",user_dutyDaos);
        //查找所有部门信息
        List<Department> departments = departmentService.selectAlldepartment();
        model.addAttribute("departments", departments);
        //查询所有员工信息
        Page<Users> users = userService.selectAlluser(pagenum);
        model.addAttribute("users",users);
        model.addAttribute("users1",users1);
        return "person_manage/所有员工";
    }
    //按条件查询员工信息
    @RequestMapping("selectone")
    public String selectone(Model model, Users users1, @RequestParam(defaultValue = "1") int pagenum) {
        //查询所有职位信息
        List<User_duty> user_dutyDaos = user_dutyService.selectUser_duty();
        model.addAttribute("user_dutyDaos",user_dutyDaos);
        //查找所有部门信息
        List<Department> departments = departmentService.selectAlldepartment();
        model.addAttribute("departments", departments);
        //按条件查询所有员工信息
        Page<Users> users = userService.selectOneuser(users1,pagenum);
        if(("".equals(users1.getNickname())||users1.getNickname()==null)&&users1.getMobile()==0&&users1.getLogoncount()==0){
            return "redirect:/personnel/select";
        }
        model.addAttribute("users1",users1);
        model.addAttribute("users",users);
        return "person_manage/所有员工";
    }

    //员工修改页面
    @RequestMapping("updateuser")
    public String updateuser(Model model, int id) {
        //查询所有职位信息
        List<User_duty> user_dutyDaos = user_dutyService.selectUser_duty();
        model.addAttribute("user_dutyDaos",user_dutyDaos);
        //查找所有部门信息
        List<Department> departments = departmentService.selectAlldepartment();
        model.addAttribute("departments", departments);
        //根据ID查询员工信息
        Users users = userService.selectOne(id);
        model.addAttribute("users", users);
        return "person_manage/员工修改";
    }

    //员工修改页面
    @RequestMapping("manageuser")
    public String manageuser(Model model, int id) {
        //根据ID查询员工信息
        Users users = userService.selectOne(id);
        model.addAttribute("users", users);
        ////根据id 查找部门
        Department department = departmentService.selectOnedepartment(users.getLogoncount());
        model.addAttribute("department",department);
        //根据ID查找这职位
        User_duty user_duty = user_dutyService.selectOneUser_duty(users.getMobile());
        model.addAttribute("user_duty",user_duty);
        //保存上级职位信息
        String user_dutys = null;
        if(user_duty.getTid()==0){
            user_dutys = "CEO";
            model.addAttribute("user_dutys",user_dutys);
        }else{
            User_duty user_duty1 = user_dutyService.selectOneUser_duty(user_duty.getTid());
            user_dutys = user_duty1.getName();
            model.addAttribute("user_dutys",user_dutys);
        }
        return "person_manage/员工管理";
    }

    //员工修改
    @RequestMapping("update")
    public String update(Model model, Users users) {
        //修改员工信息
        int i = userService.updateuser(users);
        if (i > 0) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }
        model.addAttribute("users", users);
        //查询所有职位信息
        List<User_duty> user_dutyDaos = user_dutyService.selectUser_duty();
        model.addAttribute("user_dutyDaos",user_dutyDaos);
        //查询所有部门信息
        List<Department> departments = departmentService.selectAlldepartment();
        model.addAttribute("departments", departments);
        return "person_manage/员工修改";
    }

    //员工删除
    @RequestMapping("deleteUser")
    public String deleteUser(Model model, int[] checkbox) {
        for (int i : checkbox) {
            userService.deleteUser(i);
        }
        return "redirect:/personnel/select";
    }
    //在员工管理页面删除员工信息
    @RequestMapping("deleteOneUser")
    public String deleteOneUser(Model model, int id) {
        userService.deleteUser(id);
        return "redirect:/personnel/select";
    }
}
